<?php
/**
 * @filesource modules/event/controllers/IndexController.php
 * @author Hoang
 * @abstract : List of actions in order
 *   - new
 *   - view
 *   - edit
 *   - delete
 *   - search
 */ 
class Archin_MasterController extends Clu_Controller_Action_Master
{
	public function init()
	{
		$this->mService = new archin_Model_Service();
		parent::init();
	}
	
	public function checkPerm()
	{
		$sess = new Zend_Session_Namespace(CLU_NS);
		if (strpos($sess->userPermissions, "administer archin") === false)
			die("You must <a href='/vi/login'> Login </a> first");
		
	}
	public function homeAction()
	{
	}
	
	public function newAction()
	{
		$this->checkPerm();
		$type = $this->_request->getParam('type');
		$content = ucwords($type);
		$formName = "archin_Form_" . $content; 
		$insertService = "insertNew" . $content;
		
		$this->viewParams['form'] = new $formName();
		$this->viewParams['form']->cluBuild(array(
							'action' => 'new'
		));
		
		$data = $this->_request->getParams();
		if ($this->_request->isPost() && $this->viewParams['form']->isValid($data))
		{
			$ret = $this->mService->{$insertService}($data);	
			$this->viewParams['result'] = $ret;
		}
		$this->viewParams['bgcolor'] = get_key("archin_wysiwyg_bg_color");
		$this->viewParams['mode'] = "new";
		$this->paramsForWidgets['page'] = archin_menu_get_index($type);
		$this->presenceIdentifier = $type;
	}

	public function editAction()
	{
		$this->checkPerm();
		$id = $this->_request->getParam('id');
		$type = $this->_request->getParam('type');
		$content = ucwords($type);
		$formName = "archin_Form_" . $content;
		$updateService = "update" . $content;
		
		
		$member = $this->mService->find($content, $id);
		$memberData = $member->toArray();
//		die_a($member->toArray());
		//get member details
		$this->viewParams['form'] = new $formName();
		$this->viewParams['form']->cluBuild(array(
							'action' => 'new'
		));
		$data = $this->_request->getParams();
		if ($this->_request->isPost() && $this->viewParams['form']->isValid($data))
		{
			$ret = $this->mService->{$updateService}($id, $data);	
			$this->viewParams['result'] = $ret;
		}
		elseif ($this->_request->isGet())
		{
			//populate the form
			$this->viewParams['form']->populate($memberData);
		}
		
		$this->viewParams['bgcolor'] = get_key("archin_wysiwyg_bg_color");
		$this->viewParams['mode'] = "edit";
		$this->viewParams['id'] = $id;
		$this->presenceIdentifier = $type;
		$this->paramsForWidgets['page'] = archin_menu_get_index($type);
	}
	
	public function searchAction()
	{
		$this->checkPerm();
		$type = $this->_request->getParam('type');
		if ($type == "people")
		{
			$sql = "SELECT * FROM `people` ORDER BY weight ASC";
		}
		else if ($type == "post")
		{
			$sql = "SELECT * FROM `posts`";
		}
		else if ($type == "gallery")
		{
			$sql = "SELECT * FROM `galleries` WHERE `type`='gallery'";
		}
	
		else if ($type == "project")
		{
			$category = $this->_request->getParam('category');
			if (isset($category))
				$sql = "SELECT * FROM `projects` WHERE `category`=$category ORDER BY weight ASC";
			else
				$sql = "SELECT * FROM `projects` ORDER BY weight ASC";
		}		
		$db = Zend_Db_Table::getDefaultAdapter() ;
		$ret = $db->fetchAll($sql);
//		die_a($ret);
		$this->viewParams['type'] =  $type;
		$this->viewParams['result'] =  $ret;
		$this->presenceIdentifier = $type;
		$this->paramsForWidgets['page'] = archin_menu_get_index($type);
	}
	
	public function galleryAction()
	{
		$this->checkPerm();
		$id = $this->_request->getParam('id');
		$galleryId = $this->_request->getParam('gallery_id');
		$type = $this->_request->getParam('type');
		$content = ucwords($type);
		$row = $this->mService->find($type, $id);
		
		//pas the gallery id to the gallery/widget/upload-photos        
        if ($row->gallery)
        {
			$this->paramsForWidgets["id"] = $row->gallery;
        }
        else
        {
        	$this->presenceIdentifier = 2; //Create a new gallery message
        }
        $this->paramsForWidgets["content_id"] = $id;
        $this->paramsForWidgets["content_type"] = $type;
        
	}
	
	public function uploadGalleryPhotosAction()
	{
		$this->checkPerm();
		$galleryId = $this->_request->getParam('gallery_id');
		$contentId = $this->_request->getParam('id');
		$this->m = new gallery_Model_Gallery();
		$nService = new node_Model_NodeService();
		$nodeTypeInfo = $nService->getNodeTypeInfo("gallery");
		$ret = $nService->findNodeByPrimary($nodeTypeInfo, $galleryId);
		if ($ret)
			$gallery = $ret;
		else
			exit();			
		$posterId = $gallery['poster_id'];
		
		$type = $this->_request->getParam('type');
		//$content = ucwords($type);
		//$row = $this->mService->find($type, $id);
		//move uploaded files here
		$files = $_FILES['user_files'];

		$ustoreDir = APP_PATH."/public/ustore/$posterId/images/original/gallery/$galleryId/";
		if (!file_exists($ustoreDir))
			mkdir($ustoreDir, 0777  , true);
		
		$ufilesDir = APP_PATH."/public/ufiles/$posterId/images/original/gallery/$galleryId/" ;
		if (!file_exists($ufilesDir))
			mkdir($ufilesDir, 0777  , true);
		
		foreach ($files['name'] as $idx => $fileName)
		{
//			die($fileName);
			//make sure the file name is unique
			if (!$fileName)
				continue;
			$targetFile = $ustoreDir . $fileName;
			$i = 1;
			while(file_exists($targetFile))
			{
				$fileName = $i . "_" . $files['name'][$idx];
				$targetFile = $ustoreDir . $fileName;	
				$i++;
			}
//			die($targetFile);
			move_uploaded_file($files['tmp_name'][$idx],$targetFile);
			chmod ($targetFile, 777);
			//update $gallery
			$ret = generate_image_copies ("ustore", $ustoreDir,$fileName, null, null, null, $posterId, false); //with wm		
			if ($ret == false)
			{
				//remove the uploaded files
				unlink ($targetFile);
				exit();
			}
			
			$this->m->updateGalleryImageList($gallery, $targetFile);
		}		
		//die("/vi/archin/master/gallery?type=$type&id=$contentId&gallery_id=$galleryId");
		if ($type == "gallery")
			header("Location: /vi/gallery/upload-photos?id=$galleryId");
		else
		header("Location: /vi/archin/master/gallery?type=$type&id=$contentId&gallery_id=$galleryId");
	}
	public function apiAction()
	{
		//nothing to do
		$this->paramsForWidgets['page'] = archin_menu_get_index("api");
	}	
}
?>
